<?php

namespace app\controller\admin;

use app\bll\HomeMenu as BllHomeMenu;
use app\bll\HomeMenuCate as BllHomeMenuCate;
use core\controller\AdminBase;
use Exception;
use support\View;

class HomeMenu extends AdminBase
{
    public function menus()
    {
        $bllHomeMenuCate = new BllHomeMenuCate();
        $cates           = $bllHomeMenuCate->select(request()->nowsiteid);

        if (request()->get('type', '') == 'selmenus') {
            $bllHomeMenu = new BllHomeMenu();
            $menus       = $bllHomeMenu->cascader(
                cid: request()->get('cid', $cates ? $cates[0]['id'] : 0),
                siteid: request()->nowsiteid,
                tojson: true
            );
            return $this->success(json_decode($menus, true));
        } else {
            View::assign('cates', $cates);
            return $this->view();
        }
    }
    public function cates()
    {
        $bllHomeMenuCate = new BllHomeMenuCate();
        $cates           = $bllHomeMenuCate->select(request()->nowsiteid);

        View::assign('cates', $cates);
        return $this->view();
    }

    /**
     * 添加分类
     *
     * @return mixed
     */
    public function addcate()
    {
        if (is_post()) {
            try {
                $data            = request()->post();
                $data['siteid']  = request()->nowsiteid;
                $bllHomeMenuCate = new BllHomeMenuCate();
                $bllHomeMenuCate->add($data);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            return $this->view();
        }
    }

    public function editcate()
    {
        $bllHomeMenuCate = new BllHomeMenuCate();
        if (is_post()) {
            try {
                $bllHomeMenuCate->edit(
                    request()->get('id'),
                    request()->nowsiteid,
                    request()->post()
                );
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            $cateinfo = $bllHomeMenuCate->find(request()->get('id'), request()->nowsiteid);
            View::assign('cateinfo', $cateinfo);
            return $this->view();
        }
    }

    /**
     * 删除分类
     *
     * @return mixed
     */
    public function delcate()
    {
        try {
            $bllHomeMenuCate = new BllHomeMenuCate();
            $bllHomeMenuCate->delete(
                request()->get('id'),
                request()->nowsiteid
            );
            return $this->success();
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
    }

    public function addmenu()
    {
        $bllHomeMenu     = new BllHomeMenu();
        if (is_post()) {
            try {
                $data = request()->post();
                $data['siteid'] = request()->nowsiteid;
                $bllHomeMenu->add($data);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {


            if (request()->get('type') == 'selmenus') {

                $menus = $bllHomeMenu->cascader(
                    request()->get('cid'),
                    request()->nowsiteid
                );
                return $this->success(json_decode($menus, true));
            }



            $bllHomeMenuCate = new BllHomeMenuCate();
            $cates           = $bllHomeMenuCate->select(request()->nowsiteid);

            View::assign('cates', $cates);
            return $this->view();
        }
    }

    public function editemenu()
    {
        $id          = request()->get('id');
        $bllHomeMenu = new BllHomeMenu();
        $menuinfo    = $bllHomeMenu->find($id, request()->nowsiteid);
        if (!$menuinfo) {
            throw new Exception('信息不存在');
        }
        if (is_post()) {
            try {
                $bllHomeMenu
                    ->edit(
                        $id,
                        request()->nowsiteid,
                        request()->post()
                    );
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {


            $menus = $bllHomeMenu->cascader(
                $menuinfo['cid'],
                request()->nowsiteid
            );

            $bllHomeMenuCate = new BllHomeMenuCate();
            $cates           = $bllHomeMenuCate->select(request()->nowsiteid);

            View::assign('cates', $cates);
            View::assign('menus', $menus);
            View::assign('menuinfo', $menuinfo);
            return $this->view();
        }
    }


    /**
     * 删除菜单
     *
     * @return mixed
     */
    public function delmenu()
    {
        try {
            $bllHomeMenu = new BllHomeMenu();
            $bllHomeMenu->delete(request()->get('id'), request()->nowsiteid);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }


    public function sort()
    {
        try {
            $bllHomeMenu = new BllHomeMenu();
            $bllHomeMenu->sort(
                id: request()->input('id'),
                sort: request()->input('sort'),
                siteid: request()->nowsiteid
            );
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
